# 1.导包
import pandas as pd
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier         # 随机森林
from sklearn.tree import DecisionTreeClassifier             # 分类决策树
from sklearn.ensemble import GradientBoostingClassifier     # GBTD梯度提升树


# 2.读取数据
df = pd.read_csv("train.csv", encoding="UTF-8")
# print(df.info())

# 3.数据预处理
# 3.1 填充空值
df["Age"] = df["Age"].fillna(df["Age"].mean())    # 将年龄的平均数据填充到年龄列的空值
# print(df.info())

# 3.2 删除对目标值没有价值的特征列
new_df = df.drop(["PassengerId", "Name", "Ticket", "Cabin", "Embarked"], axis=1)

# # 3.3 进行热编码处理
new_df = pd.get_dummies(new_df)     # 将性别转变成两列，分别为：Sex_female、Sex_male

# 3.4 获取特征值、目标值
x_data = new_df.iloc[:, 1:-1]
y_data = new_df.iloc[:, 0]

# 3.5 划分数据集
x_train, x_test, y_train, y_test = train_test_split(x_data, y_data, test_size=0.2, random_state=922, stratify=y_data)

# 4.特征工程
# 4.1 特征预处理
transformer = StandardScaler()
x_train = transformer.fit_transform(x_train)    # 标准化特征训练数据
x_test = transformer.transform(x_test)          # 标准化特征测试数据

# 5.模型构建和预测
model1 = DecisionTreeClassifier(criterion="gini", max_depth=10, random_state=727)
model1.fit(x_train, y_train)             # 模型训练
y_predict = model1.predict(x_test)       # 模型预测
print("普通的决策树_准确度得分", accuracy_score(y_test, y_predict))

# 5.2- 随机森林
"""
    n_estimators：设置随机森林中弱学习器(基学习器)的个数
    criterion：使用哪种决策树
    max_depth：随机森林中每棵决策树的最大深度上限是多数
"""
model2 = RandomForestClassifier(criterion="gini", max_depth=10, random_state=727, n_estimators=140)
model2.fit(x_train, y_train)
y_predict = model2.predict(x_test)
print("随机森林_准确度得分", accuracy_score(y_test, y_predict))

# 5.3- 随机森林+交叉验证和网格搜索
# estimator = RandomForestClassifier()
# param_dict = {
#     "criterion": ["gini","entropy"],
#     "max_depth": [10*i for i in range(1,5)],
#     "random_state": [727],
#     "n_estimators": [20*i for i in range(1,21)]
# }
# model3 = GridSearchCV(estimator=estimator,param_grid=param_dict,cv=4)
# model3.fit(x_train, y_train)
# print(model3.best_score_)
# print(model3.best_params_)
# y_predict = model3.predict(x_test)
# print("随机森林_准确度得分", accuracy_score(y_test, y_predict))

model3 = GradientBoostingClassifier(max_depth=10, random_state=727, n_estimators=10)
model3.fit(x_train, y_train)             # 模型训练
y_predict = model3.predict(x_test)       # 模型预测
print("GBTD得分", accuracy_score(y_test, y_predict))


# 7.模型评估